char a[1002][1002];r[99999],s[99999];main(n,i,j,x,y,e,w,t,l){for(gets(&n);~scanf("%d\n",&n);printf(e?"%d\n":"pitty!\n",e+1)){for(i=1;i<=n;i++){*a[i]='@';gets(a[i]+1);strcpy(a[i]+strlen(a[i]),"@"); }*r=1;r[1]=1;memset(a[n+1],'@',n+2);a[n][n]=a[n][n]-'O'?'@':'#';for(w=t=e=l=0;!e;l++,t=0){while(~w){x=r[2*w],y=r[2*w--+1];a[y][x]='@';if(a[y][x+1]=='#'||a[y][x-1]=='#'||a[y+1][x]=='#'||a[y-1][x]=='#')e=l;if(a[y][x+1]=='O')s[t*2]=x+1,s[t++*2+1]=y,a[y][x+1]='@';if(a[y][x-1]=='O')s[t*2]=x-1,s[t++*2+1]=y,a[y][x-1]='@';if(a[y+1][x]=='O')s[t*2]=x,s[t++*2+1]=y+1,a[y+1][x]='@';if(a[y-1][x]=='O')s[t*2]=x,s[t++*2+1]=y-1,a[y-1][x]='@';}w=t-1;if(!t)break;memcpy(r,s,8*t);}}}
